import React, { Component } from 'react';
import Navigation from './Navigation';
// import { Avatar } from 'antd';
import { getData, getQueryVariable, getData2 } from "../js/fetch";
import Alert from './alert_dialog_wrapper';
import '../scss/Header.scss';
import IC0102 from '../images/IC0102.png';
import IC0101 from '../images/教育局管理员.png';

class Header extends Component {
    constructor(props) {
        super(props);
        console.log("传入参数:", props)
        this.state = {
            userName: "",
            photoPath: "",
            data: {},
            isOpen: false,
            ProductName: '校园公告信息管理',
            identity: {},
            eduIdPath: '',
            isPreset: sessionStorage.getItem('isPreset'),
            iconUrl: sessionStorage.getItem('iconUrl'),
            identityName: sessionStorage.getItem('identityName')
        }
    }
    componentDidMount() {
        if (getQueryVariable('sysID') && window.location.href.split('?')[0].indexOf('#/RecNoticeContent') > -1) {
            return
        }
        let token = '';
        if (!sessionStorage.getItem('token')) {
            token = getQueryVariable('lg_tk');
            sessionStorage.setItem('token', token);
        } else {
            token = sessionStorage.getItem('token');
        }
        this.setState({
            userName: decodeURIComponent(sessionStorage.getItem('userName')),
            photoPath: decodeURIComponent(sessionStorage.getItem('photoPath')),
            ProductName: sessionStorage.getItem('ProductName'),
        }, () => {
            // sessionStorage.setItem('PsnMgrToken', sessionStorage.getItem('token'));
            let UEditorLooding = setInterval(() => {
                if (sessionStorage.getItem('PsnMgrLooding') == 1) {
                    let PsnMgrLgAssistantAddr = sessionStorage.getItem('PsnMgrLgAssistantAddr');
                    this.loadScriptOrCss([PsnMgrLgAssistantAddr + "/PsnMgr/LgAssistant/css/lancoo.cp.assistantInfoCenter.css",
                    PsnMgrLgAssistantAddr + "/PsnMgr/LgAssistant/assets/jquery.pagination.js",
                    PsnMgrLgAssistantAddr + "/PsnMgr/LgAssistant/js/lancoo.cp.assistantInfoCenter.js",
                    ]);
                    clearInterval(UEditorLooding);
                }
            }, 500);
        })
    }
    changeIsOpen() {
        this.setState({
            isOpen: true
        })
    }
    //帮助
    openHelp() {
        console.log('window.location.href', window.location);
        if (this.props.headerStyle == 3) {
            //开发环境
            if (window.location.href.indexOf('http://localhost:300') === 0) {
                let origin = window.location.origin
                window.open(`${origin}/UserHelp/index.html?lg_tk=` + getQueryVariable('lg_tk') + '&lg_ic=' + getQueryVariable('lg_ic'), "_blank");
            } else {
                let href = window.location.href
                let index = href.indexOf('/html')
                let result = href.substring(0, index);
                window.open(`${result}/UserHelp/index.html?lg_tk=` + getQueryVariable('lg_tk') + '&lg_ic=' + getQueryVariable('lg_ic'), "_blank");
            }
        }
    };
    loadScriptOrCss(urlArr, num) {
        let $this = this;
        if (!num) {
            num = 0;
        }
        //正则判断是否是css;
        var reg = RegExp(/.css/);

        if (reg.test(urlArr[num])) {
            // 动态生成css
            var scriptOrCss = document.createElement('link');
            scriptOrCss.type = 'text/css';
            scriptOrCss.async = 'async';
            scriptOrCss.href = urlArr[num];
            scriptOrCss.rel = "stylesheet";
            document.getElementsByTagName("head")[0].appendChild(scriptOrCss);

        } else {
            // 动态生成js
            var scriptOrCss = document.createElement('script');
            scriptOrCss.type = 'text/javascript';
            scriptOrCss.async = 'async';
            scriptOrCss.src = urlArr[num];
            document.body.appendChild(scriptOrCss);
        }
        if (scriptOrCss.readyState) {
            //IE下
            scriptOrCss.onreadystatechange = function () {
                if (scriptOrCss.readyState === 'complete' || scriptOrCss.readyState === 'loaded') {
                    scriptOrCss.onreadystatechange = null;
                    num++;
                    if (num === urlArr.length) {
                        return;
                    } else {
                        $this.loadScriptOrCss(urlArr, num);
                    }
                }
            }
        } else {
            //其他浏览器
            scriptOrCss.onload = function () {
                num++;
                if (num === urlArr.length) {
                    return;
                } else {
                    $this.loadScriptOrCss(urlArr, num);
                }
            }
        }
    }
    Logout(data) {
        if (data === 'true') {
            getData('api/commons/school/user/logout', {
                token: sessionStorage.getItem('token')
            }).then(data => {
                return data.json()
            }).then(json => {
                if (json.code == '200') {
                    let url = window.location.href;
                    url = url.split('?');
                    if (url.length > 1 && url[1].indexOf('&') > -1) {
                        let pram = url[1].split('&');
                        url = url[0] + '?';
                        pram.map((item) => {
                            if (item.indexOf('lg_tk=') === -1) {
                                url += item + '&';
                            }
                        })
                        url.slice(0, url.length - 1);
                    } else {
                        url = url[0];
                    }
                    if (url.slice(-2) === '#/') {
                        url = url.slice(0, -2);
                    }
                    console.log(url);
                    url = encodeURIComponent(url);
                    sessionStorage.removeItem('token');
                    // alert(  this.state.data.baseUrl +'/UserMgr/Login/Login.aspx?lg_sysid='+this.state.data.SysID+'&lg_preurl='+url)\
                    window.open(sessionStorage.getItem('baseUrl') + '/UserMgr/Login/Login.aspx', '_self');
                }
            })
        }
        this.setState({
            isOpen: false
        })
    }
    openIndex() {
        var edu = getQueryVariable('edu')
        var UserType = sessionStorage.getItem('UserType')
        var UserClass = sessionStorage.getItem('UserClass')
        var token = sessionStorage.getItem('token')
        if (edu) {
            if (UserType == 6 && UserClass == 2) {
                window.open(sessionStorage.getItem('baseUrl') + '/Index_A_aspx?lg_tk=' + token, '_blank');
            }
        } else {
            window.open(sessionStorage.getItem('baseUrl'), '_self');
        }
    }
    gotoPersonalMgr() {
        var edu = getQueryVariable('edu')
        var token = sessionStorage.getItem('token')
        if (edu) {
            window.open(sessionStorage.getItem('baseUrl') + '/UserMgr/PersonalMgr/Default.aspx?lg_tk=' + token, '_blank');
        } else {
            window.open(sessionStorage.getItem('baseUrl') + '/html/personalMgr/', '_blank');
        }
    }
    render() {
        let className1 = '';
        let name = '';
        let Engname = '';
        let LitleName = '';

        let {
            identity,
            eduIdPath,
            identityName,
            iconUrl,
            isPreset
        } = this.state;
        if (this.props.headerStyle === 3) {
            className1 = 'notice-header-logn notice-header-logn1  newsHeader';
            name = '公告栏管理';
            Engname = 'News and Information';
        }
        var edu = getQueryVariable('edu')
        var UserType = sessionStorage.getItem('UserType')
        var UserClass = sessionStorage.getItem('UserClass')

        return (
            <div className={this.props.headerStyle === 3 ? "notice-header news-header" : "notice-header"}>
                <div className="notice-header-top">
                    <div className={this.props.headerStyle === 3 ? "box-middle box-middle1" : "box-middle"}>
                        <div className="notice-header-right">
                            <span className='login_back'></span>
                            <span onClick={() => { this.openIndex() }}>返回办公平台</span>
                            {
                                edu ? null : <div className={this.props.headerStyle === 3 ? 'news-line' : 'notice-header-border'} ></div>
                            }
                            {/* 小铃铛 */}
                            <span className='assi_head'>
                                <div id="Assistant_infoCenter"></div>
                                <span onClick={() => document.getElementById('Assistant_infoCenter').click()} >消息</span>
                            </span>
                     
                            {/* {
                                (sessionStorage.getItem('UserType') == '0' || getQueryVariable('lg_ic') == 'IC0002') && this.props.headerStyle === 3 ?
                                    <span className='assi_head' onClick={() => this.openHelp()}>
                                        <i className="helpIcon"></i>
                                        <span >帮助</span>
                                    </span>
                                    : ''
                            } */}
                            {
                                edu ? null : <div className={this.props.headerStyle === 3 ? 'news-line' : 'notice-header-border'} ></div>
                            }
                            <div className='notice-header-sculpture' style={{ 'backgroundImage': `url(${this.state.photoPath})` }} onClick={() => { this.gotoPersonalMgr() }}></div>
                            <div className='notice-header-name' onClick={() => { this.gotoPersonalMgr() }} >{this.state.userName}</div>
                            {
                                edu ?
                                    (<img src={eduIdPath} />)
                                    : (
                                        isPreset ?
                                            iconUrl ?
                                                <img src={iconUrl} style={{
                                                    margin: '-4px 5px 0 3px'
                                                }} /> :
                                                <div className="iconUrl-container" >
                                                    <img src={iconUrl} />
                                                    {identityName}
                                                </div> :
                                            ""
                                    )

                            }

                            <i onClick={() => { this.changeIsOpen() }}></i>
                        </div>
                        <div >
                            <span className={className1}  ></span>
                            <span className={this.props.headerStyle === 3 ? "notice-header-title notice-header-title1" : "notice-header-title"} style={{ left: (this.props.headerStyle === 2 ? '88px' : '') }}>{this.props.headerName ? this.props.headerName : name}</span> <span className='header-litle'>{LitleName}</span>
                            <span id="circle-title" style={{
                                display: (this.props.type == "circle" ? "block" : "none")
                            }}>{this.props.circleText}</span>
                        </div>
                    </div>
                    <ul className='frame-triangle-wrapper'>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                    <Alert message='您确定要退出登录么?' isOpen={this.state.isOpen} chooseFn={(data) => { this.Logout(data) }}></Alert>
                    <Navigation />
                </div>
            </div>
        )

    }

}

export default Header;